//
//  CarTypeDetailViewController.m
//  怎么说呢
//
//  Created by 杜远洋 on 15-1-4.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "DetailCarTypeViewController.h"
#import "CarTypeTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "NextDetailViewController.h"
@interface DetailCarTypeViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation DetailCarTypeViewController
- (void)dealloc
{
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createCollection];
//    [self.view setBackgroundColor:[UIColor clearColor]];
    
}
- (void)createCollection
{
#pragma mark - 与testViewController的坐标相对应，并且实现cell的自定义高度 根据数据的多少显示多少个cell
    
    NSInteger count = 0;
    NSArray *seriesArray = [NSArray array];
    for (int i = 0; i < [self.tableArray count]; i++) {
        NSDictionary *dic = [self.tableArray objectAtIndex:i];
        seriesArray = [dic objectForKey:@"series"];
        count = count + [seriesArray count];
    }
    
    CGFloat myHeight = 0;
    if (count >= 5) {
        myHeight = self.view.frame.size.height * 2 / 3 - 44;
    }
    else
    {
        myHeight = self.view.frame.size.width * 16 / 75 * count + [self.tableArray count] * 20;
    }
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - self.view.frame.size.width/10, myHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CarTypeTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView setRowHeight:self.view.frame.size.width * 16 / 75];
    [self.view addSubview:_tableView];
    [_tableView release];
    [self.view setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.4]];
    [self.tableView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.1]];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.tableArray count];
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *dic = [self.tableArray objectAtIndex:section];
    NSString *name = [dic objectForKey:@"subBrandName"];
    return name;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = [self.tableArray objectAtIndex:section];
    NSArray *array = [dic objectForKey:@"series"];
    
    
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CarTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.section];
    NSArray *array = [dic objectForKey:@"series"];
    NSDictionary *myDic = [array objectAtIndex:indexPath.row];
    [cell setBackgroundColor:[UIColor grayColor]];
    [cell.nameLabel setText:[myDic objectForKey:@"name"]];
    [cell.myImage setImageWithURL:[NSURL URLWithString:[myDic objectForKey:@"icon"] ] placeholderImage:[UIImage imageNamed:@"placeHolder.png"] options:SDWebImageRetryFailed];
    [cell.priceLabel setText:[myDic objectForKey:@"price"]];
   cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.section];
    NSArray *array = [dic objectForKey:@"series"];
    NSDictionary *myDic = [array objectAtIndex:indexPath.row];
    
    NextDetailViewController *next = [[NextDetailViewController alloc] init];
    [self.navigationController pushViewController:next animated:YES];
    
    NSNumber *numberId = [myDic objectForKey:@"id"];
    NSInteger number = [numberId integerValue];
    next.myCarId = number;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
